package com.beifu.manager.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.beifu.manager.model.Organizer;
import com.beifu.manager.model.OrganizerExample;
import com.beifu.manager.model.OrganizerExample.Criteria;
import com.beifu.manager.service.organization.OrganizerService;
import com.beifu.manager.shiro.ActiveUser;
import com.mds.manager.utils.PageUtils;
import com.mds.manager.utils.ResponseHelp;
import com.utils.Constants;

@Controller
public class StorageManagerController {

	private Logger Log = Logger.getLogger(this.getClass());
	@Autowired
	private OrganizerService organizerService;
	
	
	@RequestMapping("/storage/goStorageIndex")
	public String toStorageManagerPage(Model model, HttpServletRequest request){
		Log.info("======>>>进入库房管理列表界面");
		
		return "/storagemanager/storage_index";
	}
	
	/**
	 * 库房列表数据
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/storage/getStoragePage",produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
	public String getPageData(Model model,HttpServletRequest request, HttpServletResponse res){
		String page = request.getParameter("pageNumber");
		String contacts = request.getParameter("contacts");//库房联系人
		String organizeName = request.getParameter("organizeName");//库房名称
		
		OrganizerExample organizerExample = new OrganizerExample();
		Criteria criteria = organizerExample.createCriteria();
		
		if(StringUtils.isNotBlank(contacts)){
			criteria.andContactsLike("%"+contacts+"%");
		}
		
		if(StringUtils.isNotBlank(organizeName)){
			criteria.andOrganizeNameLike("%"+organizeName+"%");
		}
		
		List<Integer> organizerTypes = new ArrayList<Integer>();
		organizerTypes.add(Constants.ORGANIZER_TYPE.Paragraph);
		organizerTypes.add(Constants.ORGANIZER_TYPE.Storage);
		criteria.andOrganizeTypeIn(organizerTypes);
		
		PageUtils Page = new PageUtils(page);
		Page.setQueryParames(organizerExample);
		Page = organizerService.getOrganizerPages(Page);
		return ResponseHelp.responseText(Page);
	}
	
	@RequestMapping(value="/storage/goStorageDetail",produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
	public String goStorageDetail(HttpServletRequest request, HttpServletResponse res){
		
		String id = request.getParameter("id");
		Organizer organizer = null;
		
		if(StringUtils.isNotBlank(id)){
			organizer = organizerService.selectByPrimaryKey(Integer.valueOf(id));
		}
		if(null != organizer){
			request.setAttribute("organizer", organizer);
		}
		
		return "/storagemanager/storage_detail";
	}
	
	@ResponseBody
	@RequestMapping("/storage/storageSave")
	public String assetInAdd(Model model, HttpServletRequest request){
		Log.info(">> 保存库房信息");
		
		String id = request.getParameter("id");
		
		String contacts = request.getParameter("contacts");
		String organizeName = request.getParameter("organizeName");
		String contactsPhone = request.getParameter("contactsPhone");
		String organizeAddress = request.getParameter("organizeAddress");
		String organizeType = request.getParameter("organizeType");
		
		
		
		Organizer organizer  = new Organizer();
		organizer.setContacts(contacts);
		organizer.setOrganizeName(organizeName);
		organizer.setContactsPhone(contactsPhone);
		organizer.setOrganizeAddress(organizeAddress);
		organizer.setOrganizeType(Integer.valueOf(organizeType));
		
		if(StringUtils.isNotBlank(id)){
			organizer.setId(Integer.valueOf(id));
			organizerService.updateByPrimaryKeySelective(organizer);
		}else{
			organizerService.insertSelective(organizer);
		}
		return ResponseHelp.responseText();
	}
}
